<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div slot="header" class="clearfix">
        <span>专业筛选</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleSearchList()">
          <i class="el-icon-search"></i>搜索
        </el-button>
      </div>
      <div class="filter-item">
        <el-input
          v-model="listQuery.keyword"
          placeholder="专业名称"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          clearable
        />
        <el-button @click="handleSearchList()" style="margin-left: 10px;">搜索</el-button>
        <el-button @click="listQuery = Object.assign({}, defaultListQuery); handleSearchList()">重置</el-button>
      </div>
      <div class="action-buttons">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAddMajor"
        >
          添加专业
        </el-button>
      </div>
    </el-card>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <div class="table-container">
      <el-table
        ref="majorTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="专业名称" min-width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="专业代码" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.code }}</template>
        </el-table-column>
        <el-table-column label="所属院系" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.departmentName }}</template>
        </el-table-column>
        <el-table-column label="学制" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.duration }}年</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdateMajor(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleViewClasses(scope.$index, scope.row)"
            >
              班级
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDeleteMajor(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.size"
        :total="total"
      />
    </div>

    <!-- 添加专业对话框 -->
    <el-dialog
      title="添加专业"
      :visible.sync="addDialogVisible"
      width="500px"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addRules"
        label-width="100px"
      >
        <el-form-item label="专业名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业代码" prop="code">
          <el-input v-model="addForm.code" placeholder="请输入专业代码" />
        </el-form-item>
        <el-form-item label="所属院系" prop="departmentId">
          <el-select v-model="addForm.departmentId" placeholder="请选择院系" clearable>
            <el-option
              v-for="item in departmentOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学制" prop="duration">
          <el-input v-model.number="addForm.duration" type="number" placeholder="请输入学制(年)" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="addForm.status" placeholder="请选择状态">
            <el-option label="禁用" :value="0" />
            <el-option label="启用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑专业对话框 -->
    <el-dialog
      title="编辑专业"
      :visible.sync="editDialogVisible"
      width="500px"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        label-width="100px"
      >
        <el-form-item label="专业名称" prop="name">
          <el-input v-model="editForm.name" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业代码" prop="code">
          <el-input v-model="editForm.code" placeholder="请输入专业代码" />
        </el-form-item>
        <el-form-item label="所属院系" prop="departmentId">
          <el-select v-model="editForm.departmentId" placeholder="请选择院系" clearable>
            <el-option
              v-for="item in departmentOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学制" prop="duration">
          <el-input v-model.number="editForm.duration" type="number" placeholder="请输入学制(年)" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editForm.status" placeholder="请选择状态">
            <el-option label="禁用" :value="0" />
            <el-option label="启用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMajorPage, deleteMajor, addMajor, getMajorById, updateMajor } from '@/api/edu'

const defaultListQuery = {
  page: 1,
  size: 10,
  keyword: null
}

export default {
  name: 'MajorList',
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: 0,
      listLoading: false,
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        name: '',
        code: '',
        departmentId: '',
        duration: '',
        status: 1
      },
      editForm: {
        id: null,
        name: '',
        code: '',
        departmentId: '',
        duration: '',
        status: 1
      },
      addRules: {
        name: [
          { required: true, message: '请输入专业名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入专业代码', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择院系', trigger: 'change' }
        ],
        duration: [
          { required: true, message: '请输入学制', trigger: 'blur' },
          { type: 'number', message: '学制必须是数字', trigger: 'blur' }
        ]
      },
      editRules: {
        name: [
          { required: true, message: '请输入专业名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入专业代码', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择院系', trigger: 'change' }
        ],
        duration: [
          { required: true, message: '请输入学制', trigger: 'blur' },
          { type: 'number', message: '学制必须是数字', trigger: 'blur' }
        ]
      },
      departmentOptions: [
        // 这里应该从API获取院系列表
        { id: 1, name: '计算机学院' },
        { id: 2, name: '软件学院' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAddMajor() {
      // 重置表单
      this.addForm = {
        name: '',
        code: '',
        departmentId: '',
        duration: '',
        status: 1
      }
      this.addDialogVisible = true
    },
    handleUpdateMajor(index, row) {
      // 获取专业详情
      getMajorById(row.id).then(response => {
        this.editForm = { ...response.data }
        this.editDialogVisible = true
      })
    },
    handleViewClasses(index, row) {
      this.$router.push(`/edu/major/${row.id}/classes`)
    },
    handleDeleteMajor(index, row) {
      this.$confirm('是否要删除该专业?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMajor(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      getMajorPage(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.records
        this.total = response.data.total
      }).catch(() => {
        this.listLoading = false
      })
    },
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addMajor(this.addForm).then(response => {
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 1000
            })
            this.addDialogVisible = false
            this.getList()
          })
        }
      })
    },
    submitEditForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateMajor(this.editForm).then(response => {
            this.$message({
              message: '更新成功',
              type: 'success',
              duration: 1000
            })
            this.editDialogVisible = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px;
}

.operate-container {
  margin-bottom: 15px;
}

.table-container {
  margin-bottom: 15px;
}

.pagination-container {
  display: inline-block;
  float: right;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;
  margin-bottom: 10px;
}
</style>